package com.bw.a2209clubshop.base

import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.bw.a2209clubshop.R
import com.bw.a2209clubshop.ui.mall.adapter.MallHomeMultiAdapter
import com.bw.a2209clubshop.ui.mall.model.MallModel
import com.bw.a2209clubshop.utils.ErrorEmptyUIUtils

import com.scwang.smart.refresh.layout.SmartRefreshLayout

abstract class BaseMallHomeDataFragment<VM : BaseViewModel, VDB : ViewDataBinding> : BaseFragment<VM, VDB>() {
    var mAdapter: MallHomeMultiAdapter? = null
    var mItems = ArrayList<MallModel>()
    var smartRefreshLayout: SmartRefreshLayout? = null

    var emptyTips = ErrorEmptyUIUtils.EMPTY_TIPS
    var errorTips = ErrorEmptyUIUtils.ERROR_TIPS
    val emptyView by lazy {
        layoutInflater.inflate(R.layout.view_empty_ui, null, false)
    }

    override fun setLoadingViewVisible(show: Boolean, msg: String) {
        super.setLoadingViewVisible(show, msg)
        if (!show) this.smartRefreshLayout?.finishRefresh()
    }

    abstract fun initAdapter()

    // 从网络获取数据
    open fun getDataFromNet(onRefresh: Boolean = false) {
        if (!onRefresh)
            setLoadingViewVisible(true, "数据加载中...")
    }

    fun attachSmartRefreshLayout(smartRefreshLayout: SmartRefreshLayout) {
        this.smartRefreshLayout = smartRefreshLayout
        smartRefreshLayout.setOnRefreshListener {
            onRefresh()
        }
    }

    fun onLoadError() {
        if (mItems.isNullOrEmpty()) {
            mAdapter?.data?.clear()
            mAdapter?.setEmptyView(emptyView)
            mAdapter?.notifyDataSetChanged()
        }
    }

//     展示数据
    open fun invalidateList(resultList: List<MallModel>, pages: Int = 0) {
        ErrorEmptyUIUtils.updateEmptyUI(
            emptyView = emptyView,
            pages = pages,
            errorTips = errorTips,
            emptyTips = emptyTips,
            onRefresh = {
                onRefresh()
            }
        )

        mItems.clear()
        mItems.addAll(resultList)

        if (mAdapter == null) {
            initAdapter()
        } else {
             mAdapter?.setNewInstance(mItems)
            mAdapter?.notifyDataSetChanged()
        }

        if (mItems.isNullOrEmpty()) {
            mAdapter?.data?.clear()
            mAdapter?.setEmptyView(emptyView)
            mAdapter?.notifyDataSetChanged()
        }
    }

    fun onRefresh() {
        if (mAdapter?.hasEmptyView() == true) {
            mAdapter?.removeEmptyView()
            mAdapter?.notifyDataSetChanged()
        }
        getDataFromNet(true)
    }

    override fun onLoadFailed(msg: String) {
        errorTips = msg
//        invalidateList(emptyList(), ErrorEmptyUIUtils.ERROR_PAGE_SIZE)
    }

    fun onLoadFailed(msg: String, retryAble: Boolean = false) {
        if (retryAble)
            errorTips = msg
        else
            emptyTips = msg
//        invalidateList(
//            emptyList(), if (retryAble) ErrorEmptyUIUtils.ERROR_PAGE_SIZE
//            else ErrorEmptyUIUtils.ERROR_PAGE_SIZE + 1
//        )
    }

    fun bindAdapter(rv: RecyclerView) {
        rv.adapter = mAdapter
    }

    override fun initData() {
        super.initData()

    }
}